package com.mypage.utils;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;

/**
 * Created by liuyongjie on 2017/6/2.
 */

public class AnimViewUtils {
    private AlphaAnimation appearAnimator;

    private AlphaAnimation disappearAnimator2;
    public int ts = 3000;

    private static AnimViewUtils instance;
    private ObjectAnimator appearAnimator3 , appearAnimator4;

    public AnimViewUtils() {
        appearAnimator = new AlphaAnimation(0, 1);
        appearAnimator.setDuration(ts);

        disappearAnimator2 = new AlphaAnimation(1, 0);
        disappearAnimator2.setDuration(ts);
    }

    public synchronized static AnimViewUtils getInstance() {
        if (instance == null) {
            instance = new AnimViewUtils();
        }
        return instance;
    }

    /**
     * 隐藏Toast
     */
    public void setHintToast(View layoutTop) {

        if (layoutTop.getVisibility() == View.VISIBLE) {

//            hideRlBack(layoutTop, disappearAnimator2);
        }
    }

    /**
     * 显示Toast
     */
    public void appearToast(View layoutTop) {

        if (layoutTop.getVisibility() == View.GONE) {
//            layoutTop.startAnimation(appearAnimator);
            layoutTop.setVisibility(View.VISIBLE);
            showRlBack(layoutTop, appearAnimator);
        }

    }

    /**
     * 控制渐进式隐藏布局
     */
    private void hideRlBack(final View rl, AlphaAnimation disappearAnimator) {
        rl.startAnimation(disappearAnimator);
        disappearAnimator.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                rl.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 控制渐进式隐藏布局
     */
    private void showRlBack(final View rl, AlphaAnimation appearAnimator) {
        rl.startAnimation(appearAnimator);
        appearAnimator.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                rl.setVisibility(View.VISIBLE);
                rl.startAnimation(disappearAnimator2);
                disappearAnimator2.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        rl.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 显示Toast3秒后隐藏--对应标准化的弹窗控件
     */
    public void appearToast2(View layoutTop) {

        layoutTop.setVisibility(View.VISIBLE);
        showRlBack(layoutTop);

    }


    /**
     * 控制渐进式隐藏布局
     */
    private void showRlBack(final View rl) {
        appearAnimator3 = ObjectAnimator.ofFloat(rl, "alpha", 1f, 0f);
        appearAnimator3.setDuration(500);
        appearAnimator3.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                rl.setVisibility(View.GONE);
                appearAnimator4 = ObjectAnimator.ofFloat(rl, "alpha", 0f, 1f);
                appearAnimator4.setDuration(2);
                appearAnimator4.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        appearAnimator3.setStartDelay(ts);
        appearAnimator3.start();
    }
}
